package com.srwl.mytx.widget;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.srwl.mytx.R;


public class ConfirmDialog extends AlertDialog {

    private final String content;
    private String confirmButtonText;
    private String cancelButtonText;
    private String title;
    private View rl_confirm, rl_cancel, placeHolder;
    private ConfirmListener confirmListener;
    private CancelListener cancelListener;
    private boolean showCancel;
    private int countDown = -1;
    private TextView tv_content;
    private Handler handler = new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            tv_content.setText(String.format(content, countDown));
            countDown--;
            if (countDown != 0) {
                handler.postDelayed(runnable, 1000);

            } else {
                if (confirmListener != null) {
                    handler.postDelayed(() -> confirmListener.confirm(), 1000);
                }
            }
        }
    };

    public ConfirmDialog(@NonNull Context context, String title, String content) {
        super(context);
        this.title = title;
        this.content = content;
    }

    public ConfirmDialog(@NonNull Context context, String title, String content, boolean showCancel) {
        super(context);
        this.title = title;
        this.content = content;
        this.showCancel = showCancel;
    }

    public ConfirmDialog(@NonNull Context context, String title, String content, String confirmButtonText, String cancelButtonText, boolean showCancel) {
        super(context);
        this.title = title;
        this.confirmButtonText = confirmButtonText;
        this.cancelButtonText = cancelButtonText;
        this.content = content;
        this.showCancel = showCancel;
    }

    public ConfirmDialog(@NonNull Context context, String title, String content, String confirmButtonText, int countDown) {
        super(context);
        this.title = title;
        this.confirmButtonText = confirmButtonText;
        this.content = content;
        this.countDown = countDown;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_comfirm_dialog);
        TextView tv_title = findViewById(R.id.tv_title);
        tv_content = findViewById(R.id.tv_content);
        if (confirmButtonText != null) {
            TextView tv_confirm = findViewById(R.id.tv_confirm);
            tv_confirm.setText(confirmButtonText);
        }
        if (cancelButtonText != null) {
            TextView tv_cancel = findViewById(R.id.tv_cancel);
            tv_cancel.setText(cancelButtonText);
        }
        if (countDown != -1) {
            handler.post(runnable);
        } else {
            tv_content.setText(content);
        }
        tv_title.setText(title);
        placeHolder = findViewById(R.id.place_holder);
        rl_confirm = findViewById(R.id.rl_confirm);
        rl_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmListener != null) {
                    if (countDown != -1) {
                        handler.removeCallbacks(runnable);
                    } else {
                        confirmListener.confirm();
                    }
                    dismiss();
                } else {
                    dismiss();
                }
            }
        });

        rl_cancel = findViewById(R.id.rl_cancel);
        rl_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancelListener != null) {
                    cancelListener.cancel();
                    dismiss();
                } else {
                    dismiss();
                }
            }
        });

        if (showCancel) {
            placeHolder.setVisibility(View.VISIBLE);
            rl_cancel.setVisibility(View.VISIBLE);
        }
    }

    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public void setCancelListener(CancelListener cancelListener) {
        this.cancelListener = cancelListener;

    }

    public interface ConfirmListener {
        void confirm();
    }

    public interface CancelListener {
        void cancel();
    }


}
